package com.wswy.wyjk.ui.login.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.my.httpapi.api.baseUtils.Toast;
import com.wswy.wyjk.R;
import com.wswy.wyjk.utils.ToastUtils;


public class PicVerificationDialog extends Dialog {
    public PicVerificationDialog(Context context) {
        super(context);
    }

    public PicVerificationDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private PicVerificationDialog.OnclickPositive positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
        private String picStr;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }


        /**
         * Set the Dialog title from String
         *
         * @param picStr
         * @return
         */

        public Builder setPicStr(String picStr) {
            this.picStr = picStr;
            return this;
        }

        public Builder setNegativeButtonText(String negativeButtonText) {
            this.negativeButtonText = negativeButtonText;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         PicVerificationDialog.OnclickPositive listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         PicVerificationDialog.OnclickPositive listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(PicVerificationDialog.OnclickPositive listener) {
            this.positiveButtonClickListener = listener;
            return this;
        }


        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public PicVerificationDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final PicVerificationDialog dialog = new PicVerificationDialog(context, R.style.Dialog);
//            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
//            dialog.getWindow().clearFlags( WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            View layout = inflater.inflate(R.layout.layout_dialog_pic, null);
            dialog.addContentView(layout, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

            ImageView imageView = layout.findViewById(R.id.iv_pic);

            if (!TextUtils.isEmpty(picStr)) {
                byte[] decode = Base64.decode(picStr.split(",")[1], Base64.DEFAULT);
                Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
                imageView.setImageBitmap(bitmap);
            }

            EditText editText = layout.findViewById(R.id.et_pic);


            TextView tv_cancel = layout.findViewById(R.id.tv_cancel);
            TextView tv_determine = layout.findViewById(R.id.tv_determine);
            if (!TextUtils.isEmpty(negativeButtonText)) {
                tv_cancel.setText(negativeButtonText);
            }
            if (!TextUtils.isEmpty(positiveButtonText)) {
                tv_determine.setText(positiveButtonText);
            }
            tv_cancel.setOnClickListener(v -> {
                dialog.dismiss();
                if (negativeButtonClickListener != null)
                    negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);

            });
            tv_determine.setOnClickListener(v -> {
                if (TextUtils.isEmpty(editText.getText().toString())) {
                    Toast.show("请输入图片中的数字");
                    return;
                }
                dialog.dismiss();
                if (positiveButtonClickListener != null)
                    positiveButtonClickListener.onclick(editText.getText().toString());
            });
            dialog.setContentView(layout);
            return dialog;
        }
    }

    public interface OnclickPositive {
        void onclick(String content);
    }


}
